package gestoreImmagini;

import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.util.Log;

//SINGLETON
public class MyDbConnector extends SQLiteOpenHelper {
	
	Context c;
	private List<Logo> dbLoghi;
	
	//design pattern singleton	
	private static MyDbConnector singleton = null;

    private MyDbConnector(Context context) {
        super(context, "loghi.db", null, 3);
        c = context; 
        dbLoghi = caricaLoghi();
    }
    
    public static MyDbConnector getDbConnector(Context context){
    	if (singleton == null) singleton = new MyDbConnector(context);
    	return singleton;
    }
    
    @Override
    public void onCreate(SQLiteDatabase db) {
	    db.execSQL("CREATE TABLE IF NOT EXISTS loghi (" +
	  		"_id INTEGER PRIMARY KEY AUTOINCREMENT, "+
			"nome TEXT NOT NULL, "+
	  		"descrizione TEXT NOT NULL, "+
			"logo BLOB NOT NULL"+
	    	");"
		);
	    	    
	    Log.d("HelloImageDetection", "create tabelle loghi");
    }
    
	@Override
	public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
		// TODO Auto-generated method stub
		
	}
	
	public void cancellaTuttiLoghi(){
    	getWritableDatabase().delete("loghi", null, null); 
	}
	
	public long salvaLogoSuDb(Logo l){
		SQLiteDatabase db = getWritableDatabase();
		/*inserisco il nuovo oggetto nella tabella Loghi*/
		ContentValues cv = new ContentValues();
		cv.put("nome", l.getNome());
		cv.put("descrizione", l.getDescrizione());
		cv.put("logo", bitmapToByteArray(l.getLogo()));
		long res = db.insert("loghi", null, cv);
		db.close();
		
		dbLoghi.add(l);
		
		return res;
	}
	
	
	public int numeroLoghi(){
		SQLiteDatabase db = getReadableDatabase();
		/*recupero tutti i loghi dal database*/
		Cursor c = db.query(false, "loghi", new String[]{"_id", "nome", "descrizione", "logo"}, null, null, null, null, null, null);
		int res = c.getCount();
		c.close();
		db.close();
		return res;
	}
	
	private List<Logo> caricaLoghi(){
		List<Logo> res = new ArrayList<Logo>();
		
		SQLiteDatabase db = getReadableDatabase();
		/*recupero tutti i loghi dal database*/
		Cursor c = db.query(false, "loghi", new String[]{"_id", "nome", "descrizione", "logo"}, null, null, null, null, null, null);
		
		int nomeIdx = c.getColumnIndex("nome");
		int descrIdx = c.getColumnIndex("descrizione");
		int logoIdx = c.getColumnIndex("logo");
		
		while (c.moveToNext()){
			String n = c.getString(nomeIdx);
			String d = c.getString(descrIdx);
			byte[] l = c.getBlob(logoIdx);
			res.add(new Logo(n,d,byteArrayToBitmap(l)));
		}
		
		c.close();
		db.close();
		
		return res;
	}
	
	public List<Logo> loghiDalDb(){
		return dbLoghi;	
	}
	
	
	public static Bitmap byteArrayToBitmap(byte[] blob){
		Bitmap bmp = BitmapFactory.decodeByteArray(blob,0,blob.length);
		return bmp;
	}
	
	public static byte[] bitmapToByteArray(Bitmap bm){
		ByteArrayOutputStream stream = new ByteArrayOutputStream();
		bm.compress(CompressFormat.PNG, 100, stream);
		return stream.toByteArray();
	}

	public static Bitmap intArrayToBitmap(int[] pixels, int width, int height) {
		Bitmap res = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
		res.setPixels(pixels, 0, width, 0, 0, width, height);
		
		return res;
	}
}
